﻿using NetworkLib;
using PBO.Util;
using PBO.Network;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PBO.Network.FourPlayer;
using PBO.Network.Enumerations;

namespace PBO.Forms.NetworkForm.BattleForm.FourPlayer
{
    public partial class FourPlayerClientForm : Form
    {
        public FourPlayerClientForm()
        {
            InitializeComponent();
        }

        private string _serverAddress;
        private string _myName;
        protected byte _myPosition;
        protected FourPlayerClient _client;

        public FourPlayerClientForm(string address, string name)
        {
            InitializeComponent();

            this.Icon = Properties.Resources.PokemonBall;
            _serverAddress = address;
            _myName = name;
        }
        private void InitializeClient()
        {
            _client = new FourPlayerClient(_serverAddress);
            _client.OnConnected += Connected;
            _client.OnPositionSet += SetPosition;
            _client.OnBattleReady += StartBattle;
            _client.OnDisconnected += Disconnected;
            _client.OnConnectFail += ConnectFail;
            _client.OnMyPositionSet += SetMyPosition;
        }
        protected virtual void ConnectServer()
        {
            _client.Initialize();
            _client.RunThread();
        }

        protected virtual void Connected()
        {
            _client.Logon(0);
        }
        private void ConnectFail(NetworkException exception)
        {
            ModShare.ShowInformation(this, "无法连接到主机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Invoke(new VoidFunctionDelegate(Close));
        }
        private void Disconnected()
        {
            ModShare.ShowInformation(this, "与主机断开了连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Invoke(new VoidFunctionDelegate(Close));
        }
        protected virtual void SetPosition(byte position, string player)
        {
            SetButton(Controls.Find(string.Format("Player{0}Button", position), false)[0], string.IsNullOrEmpty(player), player);
        }
        private void SetMyPosition(byte position)
        {
            _myPosition = position;
        }

        protected virtual void StartBattle(int identity)
        {
            if ((!InvokeRequired))
            {
                _client.OnDisconnected -= Disconnected;
                if (_myPosition != 0)
                {
                    Form form = ((MainForm)MdiParent).BuildBattleClientForm(_serverAddress, _myPosition, _myName, BattleMode.Double_4P);
                    form.Show();
                }
                Close();
            }
            else
            {
                Invoke(new Action<int>(StartBattle), identity);
            }
        }

        protected void SetButton(Control control, bool enable, string txt)
        {
            if (InvokeRequired)
            {
                Invoke(new Action<Control, bool, string>(SetButton), control, enable, txt);
            }
            else
            {
                control.Enabled = enable;
                control.Text = txt;
                OnSetPosition();
            }
        }

        protected virtual void OnSetPosition()
        {
        }

        #region event

        protected virtual void OnEscape()
        {
        }

        private void FourPlayerClientForm_Load(object sender, EventArgs e)
        {
            InitializeClient();
            ConnectServer();
        }

        private void FourPlayerClientForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            OnEscape();
            if (_client != null)
            {
                _client.Stop();
                _client = null;
            }
        }

        #endregion

        #region button

        private void Player2Button_Click(object sender, System.EventArgs e)
        {
            _client.SetPosition(2, _myName);
        }

        private void Player3Button_Click(object sender, System.EventArgs e)
        {
            _client.SetPosition(3, _myName);
        }

        private void Player4Button_Click(object sender, System.EventArgs e)
        {
            _client.SetPosition(4, _myName);
        }

        #endregion

    }
}
